import * as COS from 'cos-nodejs-sdk-v5';

const cos = new COS({
  SecretId: 'AKIDzvkzDLQMF7rV3Vq3961t9HKAykNuKj2R',
  SecretKey: 'LWYTxNfiZKwRjuvoV1Z2Od9c2Ql7WsjX',
});

const uploadImage = async (fileBuffer: Buffer, type: string, path: string) => {
  try {
    const data = await cos.putObject({
      Bucket: 'hdc-res-2023-1317708264', // 存储桶名称
      Region: 'ap-guangzhou', // 存储桶所在地域
      Key: path + new Date().getTime() + `.${type.split('/')[1]}`, // 可以理解为图片存储的路径+名称(唯一) 例如：indexImages/1670050961361.png
      Body: fileBuffer, // 上传文件的内容，可以为 FileStream、字符串、Buffer, 我们这里接收二进制Buffer
      onProgress: function (progressData) {
        console.log('上传进度:', progressData);
      },
    });
    const imageUrl = `https://${data.Location}`;
    return imageUrl;
  } catch (error) {
    console.log(error);
  }
};

export default uploadImage;
